package org.jeecg.glr.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.SysUtil;
import org.jeecg.glr.entity.GlrRadioTask;
import org.jeecg.glr.mapper.GlrRadioTaskMapper;
import org.jeecg.glr.service.IGlrRadioServerService;
import org.jeecg.glr.service.IGlrRadioTaskService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.HttpRequestUtil;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @Description: 音柱定时任务
 * @Author: jeecg-boot
 * @Date:   2020-02-13
 * @Version: V1.0
 */
@Service
public class GlrRadioTaskServiceImpl extends ServiceImpl<GlrRadioTaskMapper, GlrRadioTask> implements IGlrRadioTaskService {

    @Resource GlrRadioTaskMapper glrRadioTaskMapper;

    @Autowired
    private IGlrRadioServerService glrRadioServerService;

    @Override
    public void doCreateTask(GlrRadioTask glrRadioTask) throws Exception{
        if(StringUtil.isNotEmpty(glrRadioTask.getIds())){
            String [] strIds=glrRadioTask.getIds().split(",");
            for(int i=0;i<strIds.length;i++){
                GlrRadioTask tbRadioTask = super.getById(strIds[i]);

                doSponTaskStartOrStop(tbRadioTask,glrRadioTask.getStatus());
                glrRadioServerService.doRattopTaskStartOrStop(tbRadioTask,glrRadioTask.getStatus());

            }
        }
    }

    public void doSponTaskStartOrStop(GlrRadioTask tbRadioTask,String stStatus) throws Exception{
        //如果为世邦
        if (tbRadioTask.getCompanyCode().equals("1")) {
            if ("1".equals(stStatus)){
                //停用时先停止一次
                executeSponTask(tbRadioTask,stStatus);
            }
            List<String> memberIdsList = glrRadioTaskMapper.getRadioIdByTaskId(tbRadioTask.getId());
            List<String> filePathList =glrRadioTaskMapper.getfileIdByTaskId(tbRadioTask.getId());
            String memberIds = "";
            String memberstrIds = "";
            for (String id : memberIdsList) {
                if (StringUtil.isNotEmpty(memberIds)) {
                    memberIds = memberIds + id + "<";
                } else {
                    memberIds = id + "<";
                }
            }
            for (String filePath : filePathList) {
                if (StringUtil.isNotEmpty(memberIds)) {
                    memberstrIds = memberstrIds + filePath + "<";
                } else {
                    memberstrIds =filePath + "<";
                }
            }

            Integer playTime = tbRadioTask.getPlayTime();
            if (playTime==null) {
                playTime =0;
            }
            playTime=playTime*60;//以分钟为单位了

            Map<String, String> map = new HashMap<String, String>();
            map.put("jsondata[taskname]", tbRadioTask.getName());
            map.put("jsondata[isdisable]",stStatus);
            map.put("jsondata[level]", "0");
            map.put("jsondata[creator]", SysUtil.getUserInfo().getUsername());
            map.put("jsondata[triggers]", "0*" + tbRadioTask.getStartTime() + "*" + tbRadioTask.getEndTime() + "*" + tbRadioTask.getDivision() + "*0*");
            String url = "";
            if (StringUtil.isNotEmpty(tbRadioTask.getTaskId())) {
                map.put("jsondata[taskId]", tbRadioTask.getTaskId());
                url = HttpRequestUtil.PHP_MODIFYTASKINFO_URL;
            } else {
                map.put("jsondata[dirname]", "");
                url = HttpRequestUtil.PHP_ADDTASKINFO_URL;
            }
            map.put("jsondata[commands]", "3*" + playTime + "*0*0*" + memberstrIds + "*" + memberIds + "*0*0*0**0");
            JSONObject json = HttpRequestUtil.doPost(url, map, "UTF-8");
            if (json.getString("res").equals("1")) {
                if (StringUtil.isNotEmpty(json.getString("taskId"))){
                    tbRadioTask.setTaskId(json.getString("taskId"));
                }
                //tbRadioTask.setStartStatus(glrRadioTask.getStatus());
                if (GLRConstant.GLR_RADIO_TASK_STATUS_0.equals(stStatus)){
                    tbRadioTask.setStatus(GLRConstant.GLR_RADIO_TASK_STATUS_0);
                    tbRadioTask.setStartStatus(GLRConstant.GLR_RADIO_TASK_STATUS_4);
                }
                if (GLRConstant.GLR_RADIO_TASK_STATUS_1.equals(stStatus)){
                    tbRadioTask.setStatus(GLRConstant.GLR_RADIO_TASK_STATUS_1);
                    tbRadioTask.setStartStatus(GLRConstant.GLR_RADIO_TASK_STATUS_4);
                }
                super.updateById(tbRadioTask);
            }
        }
    }

    @Override
    public void executeTask(GlrRadioTask glrRadioTask) throws Exception{
        String str[] = glrRadioTask.getIds().split(",");
        for (String id : str) {
            GlrRadioTask tbRadioTask = super.getById(id);

            executeSponTask(tbRadioTask,glrRadioTask.getStatus());

            glrRadioServerService.doRattopTaskRunningOrNot(tbRadioTask,glrRadioTask.getStatus());

        }
    }

    public void executeSponTask(GlrRadioTask tbRadioTask,String stStatus) throws Exception{
        //如果为世邦
        if (tbRadioTask.getCompanyCode().equals("1")) {
            if (StringUtil.isNotEmpty(tbRadioTask.getTaskId())) {
                Map<String, String> map = new HashMap<String, String>();
                if (stStatus.equals(GLRConstant.GLR_RADIO_TASK_STATUS_3)) {
                    map.put("jsondata[taskCommand]", "runtaskinfo");
                } else {
                    map.put("jsondata[taskCommand]", "stoptaskinfo");
                }
                map.put("jsondata[taskId]", tbRadioTask.getTaskId());
                JSONObject json = HttpRequestUtil.doPost(HttpRequestUtil.PHP_EXETASKCMD_URL, map, "UTF-8");
                if (json.getString("res").equals("1")) {
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_3.equals(stStatus)){
                        tbRadioTask.setStartStatus(GLRConstant.GLR_RADIO_TASK_STATUS_3);
                    }
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_4.equals(stStatus)){
                        tbRadioTask.setStartStatus(GLRConstant.GLR_RADIO_TASK_STATUS_4);
                    }
                    super.updateById(tbRadioTask);
                }
            }
        }
    }

    @Override
    public Boolean updateTaskById(GlrRadioTask glrRadioTask) throws Exception{
        if(StringUtil.isNotEmpty(glrRadioTask.getId())){
            if (glrRadioTask!=null){
                String startStatus=glrRadioTask.getStartStatus();
                String statu=glrRadioTask.getStatus();
                //如果为世邦
                if(GLRConstant.COMPANY_CODE_SPON.equals(glrRadioTask.getCompanyCode())) {
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_3.equals(startStatus)) {
                        //立刻停止
                        executeSponTask(glrRadioTask, GLRConstant.GLR_RADIO_TASK_STATUS_4);
                    }
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_0.equals(statu)) {
                        //任务停用
                        doSponTaskStartOrStop(glrRadioTask, GLRConstant.GLR_RADIO_TASK_STATUS_1);
                    }
                }
                //如果为雷拓
                if(GLRConstant.COMPANY_CODE_RATTOP.equals(glrRadioTask.getCompanyCode())) {
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_3.equals(startStatus)) {
                        //立刻停止
                        glrRadioServerService.doRattopTaskRunningOrNot(glrRadioTask,GLRConstant.GLR_RADIO_TASK_STATUS_4);
                    }
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_0.equals(statu)) {
                        //任务停用
                        glrRadioServerService.doRattopTaskStartOrStop(glrRadioTask,GLRConstant.GLR_RADIO_TASK_STATUS_1);
                    }
                }

                super.updateById(glrRadioTask);

                //如果为世邦
                if(GLRConstant.COMPANY_CODE_SPON.equals(glrRadioTask.getCompanyCode())) {
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_0.equals(statu)) {
                        //任务启用
                        doSponTaskStartOrStop(glrRadioTask, GLRConstant.GLR_RADIO_TASK_STATUS_0);
                    }
                    /*
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_3.equals(startStatus)) {
                        //立刻执行
                        executeSponTask(glrRadioTask, GLRConstant.GLR_RADIO_TASK_STATUS_3);
                    }*/
                }
                //如果为雷拓
                if(GLRConstant.COMPANY_CODE_RATTOP.equals(glrRadioTask.getCompanyCode())) {
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_0.equals(statu)) {
                        //任务启用
                        glrRadioServerService.doRattopTaskStartOrStop(glrRadioTask,GLRConstant.GLR_RADIO_TASK_STATUS_0);
                    }
                    /*
                    if (GLRConstant.GLR_RADIO_TASK_STATUS_3.equals(startStatus)) {
                        //立刻执行
                        glrRadioServerService.doRattopTaskRunningOrNot(glrRadioTask,GLRConstant.GLR_RADIO_TASK_STATUS_3);
                    }*/
                }
            }
        }else {
            super.updateById(glrRadioTask);
        }
        return true;
    }

    @Override
    public Boolean deleteTasks(String ids) throws Exception {
        List<String> idList=Arrays.asList(ids.split(","));
        boolean bFind=true;
        for(String taskId:idList){
            GlrRadioTask glrRadioTask = super.getById(taskId);
            if (glrRadioTask!=null){
                //如果为世邦
                if (glrRadioTask.getCompanyCode().equals("1")) {

                    //立刻停止
                    executeSponTask(glrRadioTask,GLRConstant.GLR_RADIO_TASK_STATUS_4);

                    //任务停用
                    doSponTaskStartOrStop(glrRadioTask,GLRConstant.GLR_RADIO_TASK_STATUS_1);

                    Map<String, String> map = new HashMap<String, String>();
                    map.put("jsondata[taskCommand]","deltaskinfo");
                    map.put("jsondata[taskId]",glrRadioTask.getTaskId());
                    JSONObject json = HttpRequestUtil.doPost(HttpRequestUtil.PHP_EXETASKCMD_URL, map, "UTF-8");
                    //res 是否删除计划任务成功(1:成功 0：失败)
                    if (json.getString("res").equals("1")) {
                        bFind=true;
                    }else{
                        bFind=false;
                    }
                }

                if (bFind){
                    bFind=glrRadioServerService.deleteRadioTask(glrRadioTask);
                }

                if (bFind){
                    super.removeById(taskId);
                }else{
                    return false;
                }
            }
        }

        return bFind;
    }
}
